<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="用途">
              <j-dict-select-tag placeholder="请选择用途" v-model="queryParam.used" dictCode="coupon_used"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="类型">
              <j-dict-select-tag placeholder="请选择类型" v-model="queryParam.type" dictCode="coupon_type"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="特价商品">
                <j-dict-select-tag placeholder="请选择" v-model="queryParam.withSpecial" dictCode="coupon_special"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="使用开始时间">
                <j-date placeholder="请选择使用开始时间" v-model="queryParam.validStartTime"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="使用结束时间">
                <j-date placeholder="请选择使用结束时间" v-model="queryParam.validEndTime"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="coupon_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('优惠券')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"

        class="j-table-force-nowrap"
        @change="handleTableChange">
        <!--下面这个是开启多选的  配合上面批量删除按钮使用  下面这个放回到上面-->
<!--        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"-->

        <template slot="status" slot-scope="text,record">
          <!--<div v-html="text" v-if="text=='生效'" style="color: #00DB00"></div>
          <div v-html="text" v-else-if="text=='失效'" style="color: #eb2f96"></div>
          <div v-html="text" v-else style="color: #4a4a48"></div>-->
<!--          <a-popconfirm  :title="record.status == 1?'确定失效吗?':'确定生效吗?'" @confirm="() => handleEdit(record)">
            <a-switch checked-children="生效" class="ssss" un-checked-children="失效" disabled :checked="record.status == 1?true:false" />
          </a-popconfirm>-->

<!--            <a>删除</a>-->
          <a-popconfirm title="确定改为失效状态吗?" @confirm="() => handleEdit(record)" v-if="record.status==3">
            <a-button v-html="text" type="dashed" >{{record.status_dictText}}</a-button>
          </a-popconfirm>


          <a-popconfirm title="确定改为失效状态吗?" @confirm="() => handleEdit(record)" v-else-if="record.status==1">
            <a-button v-html="text"  >{{record.status_dictText}}</a-button>
          </a-popconfirm>
          <div v-html="text" v-else style="color: #fe4dd5">{{record.status_dictText}}</div>
        </template>



        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
<!--          <a @click="handleEdit(record)">编辑</a>-->
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />
          <!--<a @click="handleEdit(record)" v-if="record.status=='3'?false:true">更改状态</a>
          <a-divider type="vertical"  v-if="record.status=='3'?false:true"/>-->

          <!-- <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <coupon-modal ref="modalForm" @ok="modalFormOk"></coupon-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CouponModal from './modules/CouponModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JDate from '@/components/jeecg/JDate.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { putAction } from '@/api/manage'

  export default {
    name: 'CouponList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      JDictSelectTag,
      JDate,
      CouponModal
    },
    data () {
      return {
        description: '优惠券管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'优惠券标题',
            align:"center",
            dataIndex: 'title'
          },
          {
            title:'图片',
            align:"center",
            dataIndex: 'icon',
            scopedSlots: {customRender: 'imgSlot'}
          },
          {
            title:'用途',
            align:"center",
            dataIndex: 'used_dictText'
          },
          {
            title:'类型',
            align:"center",
            dataIndex: 'type_dictText'
          },
          {
            title:'兑换所需积分',
            align:"center",
            dataIndex: 'redeemPoints'
          },
          {
            title:'特价商品使用',
            align:"center",
            dataIndex: 'withSpecial_dictText'
          },
          {
            title:'满金额',
            align:"center",
            dataIndex: 'withAmount'
          },
          {
            title:'减金',
            align:"center",
            dataIndex: 'usedAmount'
          },
          {
            title:'配额',
            align:"center",
            dataIndex: 'quota'
          },
          {
            title:'已领取数量',
            align:"center",
            dataIndex: 'takeCount'
          },
          {
            title:'使用说明',
            align:"center",
            dataIndex: 'rulesExplain'
          },
          // {
          //   title:'已使用数量',
          //   align:"center",
          //   dataIndex: 'usedCount'
          // },
          {
            title:'开始时间',
            align:"center",
            dataIndex: 'validStartTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'结束时间',
            align:"center",
            dataIndex: 'validEndTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'有效天数',
            align:"center",
            dataIndex: 'validDays'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status_dictText',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/activity/coupon/list",
          delete: "/activity/coupon/delete",
          deleteBatch: "/activity/coupon/deleteBatch",
          exportXlsUrl: "/activity/coupon/exportXls",
          importExcelUrl: "activity/coupon/importExcel",
          edit:"/activity/coupon/edit" //修改优惠券状态
        },
        dictOptions:{},
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      //优惠券状态修改
      handleEdit(record){
        // let params = {id:record.id,status:record.status};
        let params = {id:record.id,status:'2'};
        /*if(record.status=='1'){
          params.status = '2'
        }else {
          params.status = '1'
        }*/
        putAction(this.url.edit,params).then((res)=>{
          console.log(res)
          if(res.success){
            this.$message.success(res.message)
            this.loadData(this.ipagination.current)
          }else {
            this.$message.error(res.message)
          }

        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .ant-switch-disabled{
    cursor: pointer;
    opacity: 1;
  }
  .ssss{
    cursor: pointer !important;
  }
  .ant-switch{
    background-color: rgb(255, 73, 73);
  }
  .ant-switch-checked{
    background-color: #1890FF !important;
  }
  .ant-popover-buttons{
    display: flex !important;
    flex-direction: row;
    justify-content: center;
  }
  .ant-btn .ant-btn-sm{
    margin: 0 auto;
  }
</style>